<?php

/**
 * 后台管理员表操作模型
 * @author lifw
 */
class MAdminGroup extends MY_Model
{

    const IS_FORBIDDEN = 0;
    const NOT_FORBIDDEN = 1;

    protected $table = 'admin_group';
    private $filter = array();

    public static $GROUPTYPE = [
        '1' => '销售组类型',
        '2' => '测评组类型',
        '3' => '财务组类型',
        '4' => '人事组类型',
        '5' => '采购组类型',
    ];

    function __construct()
    {
        parent::__construct();
    }

    public static function getState($state = NULL)
    {
        $f = Array(self::NOT_FORBIDDEN => '启用', self::IS_FORBIDDEN => '禁用');
        if ($state === NULL) {
            return $f;
        }
        return element($state, $f, '');
    }

    public function getById($id){
        $query = $this->db->where('id',$id)->get($this->table);
        $result = $query->row();
        return [$result->id => $result->name];
    }

    public function getList($limit, $sort = false)
    {
        $this->db->select($this->table . '.*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();

        if ($sort) {
            $this->db->order_by('sort', 'asc');
        } else {
            $this->db->order_by('id', 'desc');
        }
        $query = $this->db->get();
        return $query->result();
    }

    public function getCount()
    {
        $this->db->select('count(distinct g.id) as num');
        $this->db->from($this->table . ' as g');
        $this->formfilter->doFilter();

        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getAllGroups()
    {
        $this->db->select($this->table . '.*');
        $this->db->distinct();
        $this->db->from($this->table);
        if ($this->_user->role_id == 2 || $this->_user->role_id == 4) {
            $this->db->where('id', $this->_user->group_id);
        }

        $query = $this->db->get();
        return $query->result();
    }

    public function getReviews()
    {
        $db =& get_instance()->db;
        $db->select('id,name');
        $db->from('admin_group');
        $db->where('status',1);
        $db->where('type in (1,2)');

        //28为超级管理员组
        $db->where(array('id <>' => 28));

        $queryResult = $db->get()->result();
        $kvArr = array();
        foreach ($queryResult as $k => $v) {
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
        return $list;
    }

    public static function getChooseReviews($group_id)
    {
        $db =& get_instance()->db;
        if ($group_id) {
            $db->select('id,name');
            $db->from('admins');
            $db->where('group_id', $group_id);
        } else {
            $db->select('id,name');
            $db->from('admins');
            $db->where('role_id in (3,5)');
        }

        $queryResult = $db->get()->result();
        $kvArr = array();
        foreach ($queryResult as $k => $v) {
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    /*根据组类型查找所有组
     * @param $type
     */
    public static function getAllGroupsByType($type)
    {
        $db =& get_instance()->db;
        $db->select('id,name');
        $db->from('admin_group');
        $db->where(array('type =' => $type, 'status =' => 1));

        //28为超级管理员组
        $db->where(array('id <>' => 28));

        $queryResult = $db->get()->result();
        $kvArr = array();
        foreach ($queryResult as $k => $v) {
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public function getAllGroupsByAmazon(){
        $db =& get_instance()->db;
        $db->select('id,name');
        $db->from('admin_group');
        $db->where(array('type =' => 1, 'status =' => 1));

        if (in_array($this->_user->role_id,[2,4])){
            $db->where('id',$this->_user->group_id);
        }

        //28为超级管理员组
        $db->where(array('id <>' => 28));

        $queryResult = $db->get()->result();
        $kvArr = array();
        foreach ($queryResult as $k => $v) {
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public function getAllGroup()
    {
        $queryResult = $this->getAll();
        $kvArr = array();
        foreach ($queryResult as $k => $v) {
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }
}
